#coding:utf-8
#----this is the module to display start menu and game menu
#----for instance create the button and create the menu
#----manual now is none
#version 0.201

import pygame,sys
#pygame.init()
import text

import define

#按钮基类，用于生成其他所有按钮
class Button(object):
    def __init__(self,image,mouse_over_image,command):
        #一个按钮有两张图片，一张是普通状态，一种是鼠标经过（选中状态）
        self.image_name = image
        self.mouse_over_image_name = mouse_over_image
        #self.scripter = scripter
        #这个按钮按下执行的命令名称
        self.command = command
        #按钮位置
        self.position = (0,0)
        
    #显示函数，参数为主屏幕是surface对象和绘制按钮的坐标还有当前鼠标位置，还有左键是否被按下
    #这些参数从主函数传入
    #同时，这个函数调用之后产生一个布尔返回值，用来返回按钮执行的操作来控制全局开关meun的开或者关
    def load(self):
        self.image = pygame.image.load(self.image_name).convert_alpha()
        self.mouse_over_image = pygame.image.load(self.mouse_over_image_name).convert_alpha()
        #图片大小，用于检测是否被选中用
        self.w,self.h = self.image.get_size()
    def test_and_show(self,surface,x,y,mouse_pos,left_button_pressed=False,text_content='',select_order=0):
        
        if mouse_pos[0]>x and mouse_pos[0]<x+self.w and mouse_pos[1]>y and mouse_pos[1]<y+self.h:
            surface.blit(self.mouse_over_image,(x,y))
            if left_button_pressed:
                #print 'left key pressed on'+self.command
                return self.action_onclick()
        else:
            surface.blit(self.image,(x,y))
        #用于为按钮提供非图片对象的文字显示功能，可以直接通过透明图像生成自带文字的按钮
        if text_content:
            self.button_text_obj = text.MyText(surface,(x,y))
            self.button_text_obj.get_text(text_content) 
            self.button_text_obj.selection_display(select_order,(0,0,0))
              
    def action_onclick(self):
        return self.command
#主菜单按钮    
class StartButton(Button):
    def __init__(self,image, mouse_over_image,command='start'):
        Button.__init__(self, image, mouse_over_image,command)



#读档菜单按钮和右键菜单公用同一个类
class LoadButton(Button):
    def __init__(self,image, mouse_over_image,command='load'):
        Button.__init__(self, image, mouse_over_image,command)

        #create_load_window()
#选项菜单按钮和右键菜单公用同一个类
class OptionButton(Button):
    def __init__(self, image, mouse_over_image,command='option'):
        Button.__init__(self, image, mouse_over_image,command)

        
class ExitButton(Button):
    def __init__(self,image, mouse_over_image,command='exit'):
        Button.__init__(self, image, mouse_over_image,command)

        
#右键菜单定义
class BackToGameButton(Button):
    def __init__(self, image, mouse_over_image,command='backtogame'):
        Button.__init__(self, image, mouse_over_image,command)
    #返回一个False值表示右键菜单右键关闭，主函数做相关处理关闭右键菜单，继续游戏进程

class SaveButton(Button):
    def __init__(self, image, mouse_over_image,command='save'):
        Button.__init__(self, image, mouse_over_image,command)


class BackToMainButton(Button):
    def __init__(self, image, mouse_over_image,command='backtomain'):
        Button.__init__(self, image, mouse_over_image,command)
    #返回一个列表，false表示右键菜单关闭，True表示主菜单打开

#游戏中的选项按钮
class ChoosingButton(Button):
    def __init__(self, image, mouse_over_image,command='Choosing'):
        Button.__init__(self, image, mouse_over_image,command)
        
class ChoosingMenu(object):
    def __init__(self):
        i = 0
        self.choosing_buttons = []
        while i<4:
            self.choosing_buttons.append(ChoosingButton(define.CHOOSING_BUTTON_TEXT_EFFECT,define.CHOOSING_BUTTON_TEXT_SELECTED))
            i += 1
        print 'choosing menu init finished'
    def load(self,surface,texts):
        self.surface = surface
        self.button_cal = len(texts)
        self.texts = texts
        tmp_number = 0
        while tmp_number<self.button_cal:
            self.choosing_buttons[tmp_number].command = tmp_number
            self.choosing_buttons[tmp_number].load()
            tmp_number += 1
        print "choosing button loaded"
    def show_and_test(self,x,y,mouse_pos,left_button_pressed,text_bg_point=(25,450)):
        text_bg = pygame.image.load('ui/window.png').convert_alpha()
        self.surface.blit(text_bg,text_bg_point)
        button_pressed = []
        height = self.choosing_buttons[0].h
        i = 0
        while i < self.button_cal:
            button_pressed.append(self.choosing_buttons[i].test_and_show(self.surface,x,y+i*height,mouse_pos,left_button_pressed,self.texts[i],0))
            i += 1
        return button_pressed
        #返回被按的按钮号码列表




class StartMenu(object):
    start_button = StartButton(define.START_BUTTON,define.START_BUTTON_ONCLICK)
    load_button = LoadButton(define.LOAD_BUTTON,define.LOAD_BUTTON_ONCLICK)
    option_button = OptionButton(define.OPTION_BUTTON,define.OPTION_BUTTON_ONCLICK)
    exit_button = ExitButton(define.EXIT_BUTTON,define.EXIT_BUTTON_ONCLICK)
    buttons =(start_button,load_button,option_button,exit_button)
    def __init__(self,scripter,surface,screen_size=(800,600)):
        self.screen_size = screen_size
        self.scripter = scripter
        self.surface = surface
    def load(self):
        #装载按钮图像
        for button in self.buttons:
            button.load()
        #绘制一个半透明的菜单背景
        self.blue_screen = pygame.Surface(define.SCREEN_SIZE)
        self.blue_screen.fill((5,15,100))
        self.blue_screen.set_alpha(100)
    def show(self,x,y,mouse_pos,left_button_pressed):
        
        w,h = self.start_button.w,self.start_button.h
        button_pressed = []
        self.surface.blit(self.blue_screen,(0,0))
        i = 0
        for button in self.buttons:
            button_pressed.append(button.test_and_show(self.surface,x,y+h*i,mouse_pos,left_button_pressed))
            i+=1
        return button_pressed
